package com.molichuxing.services.infrastructure.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.molichuxing.services.property.DealerTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.DealerBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.DealerMapper;
import com.molichuxing.services.infrastructure.dto.request.create.DealerCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.DealerModifyDto;
import com.molichuxing.services.infrastructure.dto.response.DealerDto;
import com.molichuxing.services.infrastructure.service.DealerService;
import com.molichuxing.services.property.DealerStatusEnum;

/**
 * <p>
 * 经销商基础信息 服务实现类
 * </p>
 *
 * @author zph
 * @since 2019-08-12
 */
@Service("dealerService")
public class DealerServiceImpl implements DealerService {


    @Autowired
    DealerMapper dealerMapper;

    /**
     * 获取经销商基础信息分页列表
     *
     * @param map      key:status Integer 营业状态
     *                 key:shortName String 经销商名称
     *                 key：regionId Integer 区域ID
     *                 key:startCreateTime LocalDateTime 创建开始时间
     *                 key:endCreateTime LocalDateTime 创建结束时间
     *                 key:sortName String 排序字段
     *                 key:sortWay String 排序方式 asc,desc
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Paged<DealerDto> getDealerPage(Map<String, Object> map, Integer pageNum, Integer pageSize) throws Exception {
        List<DealerBean> dealerBeanList = dealerMapper.getPage(map, pageNum, pageSize);
        long pageCount = dealerMapper.getPageCount(map);
        List<DealerDto> dealerBeanDtoList = Convert.toDealerDtoList(dealerBeanList);
        return new Paged<DealerDto>(dealerBeanDtoList, pageCount, pageSize);
    }

    /**
     * 经销商添加
     *
     * @param dealerCreateDto
     * @return
     * @throws Exception
     */
    @Override
    public Integer createDealer(DealerCreateDto dealerCreateDto) throws Exception {
        Assert.notNull(dealerCreateDto, "经销商添加失败");

        String shortName = dealerCreateDto.getShortName();
        String fullName = dealerCreateDto.getFullName();
        Boolean shortNameExist = getShortNameExist(shortName);
        if (shortNameExist) {
            throw new BizException("经销商名称已存在");
        }
        Boolean fullNameExist = getFullNameExist(fullName);
        if (fullNameExist) {
            throw new BizException("经销商全称已存在");
        }

        DealerBean dealer = Convert.toDealer(dealerCreateDto);
        dealer.setStatus(DealerStatusEnum.OPEN.getValue());
        dealerMapper.create(dealer);
        return dealer.getId();
    }

    /**
     * 经销商修改
     *
     * @param dealerModifyDto
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyDealer(DealerModifyDto dealerModifyDto) throws Exception {
        Assert.notNull(dealerModifyDto, "经销商不存在");
        Integer dealerId = dealerModifyDto.getDealerId();
        Assert.notNull(dealerId, "经销商不存在");
        DealerBean dealer = dealerMapper.getById(dealerId);
        Assert.notNull(dealer, "经销商不存在");

        String shortName = dealerModifyDto.getShortName();
        String fullName = dealerModifyDto.getFullName();
        String oldShortName = dealer.getShortName();
        String oldFullName = dealer.getFullName();
        if (!shortName.trim().equals(oldShortName)) {
            Boolean shortNameExist = getShortNameExist(shortName);
            if (shortNameExist) {
                throw new BizException("经销商名称已存在");
            }
        }

        if (!fullName.trim().equals(oldFullName)) {
            Boolean fullNameExist = getFullNameExist(fullName);
            if (fullNameExist) {
                throw new BizException("经销商全称已存在");
            }
        }

        Convert.toDealer(dealerModifyDto, dealer);
        return (dealerMapper.modifyById(dealer) > 0) ? true : false;
    }

    /**
     * 编辑经销商状态
     *
     * @param dealerId
     * @param status
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyDealerStatus(Integer dealerId, Integer status) throws Exception {
        DealerBean dealer = dealerMapper.getById(dealerId);
        Assert.notNull(dealer, "经销商不存在");
        return (dealerMapper.modifyStatusById(dealerId, status) > 0) ? true : false;
    }


    /**
     * 获取经销商信息
     *
     * @param dealerId
     * @return
     * @throws Exception
     */
    @Override
    public DealerDto getDealerById(Integer dealerId) throws Exception {
        DealerBean dealer = dealerMapper.getById(dealerId);
        return Convert.toDealerDto(dealer);
    }

    /**
     * 根据状态获取经销商数量
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<Integer, Integer> getDealerCountByStatus() throws Exception {
        List<TabDto> statusList = dealerMapper.getDealerCountByStatus();
        if (null == statusList || statusList.size() == 0) {
            return null;
        }

        Map<Integer, Integer> returnMap = new HashMap<Integer, Integer>();
        for (TabDto tabDto : statusList) {
            Integer type = tabDto.getType();
            Integer count = tabDto.getCount();
            returnMap.put(type, count);
        }

        return returnMap;
    }


    /**
     * 通过id获取经销商map集合
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @Override
    public Map<Integer, DealerDto> getDealerMapByIds(List<Integer> ids) throws Exception {
        List<DealerBean> dealerBeanList = dealerMapper.getBatchByIds(ids);
        return Convert.toDealerDtoMap(dealerBeanList);
    }

    /**
     * 通过id获取经销商集合
     *
     * @param ids
     * @return
     */
    @Override
    public List<DealerDto> getDealerDtoListByIds(List<Integer> ids) {
        List<DealerBean> dealerBeanList = dealerMapper.getBatchByIds(ids);
        return Convert.toDealerDtoList(dealerBeanList);
    }

    /**
     * 通过区域id查询经销商信息（营业中）
     *
     * @param regionIdList
     * @param dealerName
     * @return
     */
    @Override
    public List<DealerDto> getDealerByRegionId(List<Integer> regionIdList, String dealerName) {
        List<DealerBean> dealerBeanList = dealerMapper.getByRegionId(regionIdList, dealerName);
        return Convert.toDealerDtoList(dealerBeanList);
    }

    /**
     * 经销商list集合
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<DealerDto> getAllDealerList(Integer status, Integer type, Integer dealerId) throws Exception {
        List<DealerBean> dealerBeanList = dealerMapper.getAllList(status, type, dealerId);
        return Convert.toDealerDtoList(dealerBeanList);
    }

    /**
     * 获取经销商信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<DealerDto> getDealerList() throws Exception {
        List<DealerBean> dealerBeanList = dealerMapper.getAllList(null, null, null);
        return Convert.toDealerDtoList(dealerBeanList);
    }

    /**
     * 根据经销商名称模糊查询(营业中，分销)
     *
     * @param dealerName
     * @return
     */
    @Override
    public List<DealerDto> getDealerListByName(String dealerName) {
        Integer status = DealerStatusEnum.OPEN.getValue();
        Integer type = DealerTypeEnum.DISTRIBUTION.getValue();
        List<DealerBean> dealerBeanList = dealerMapper.getListByName(dealerName, status, type);
        return Convert.toDealerDtoList(dealerBeanList);
    }

    /**
     * 根据经销商名称模糊查询
     *
     * @param dealerName
     * @return
     */
    @Override
    public List<DealerDto> getAllDealerListByName(String dealerName) {
        List<DealerBean> dealerBeanList = dealerMapper.getListByName(dealerName, null, null);
        return Convert.toDealerDtoList(dealerBeanList);
    }

    /**
     * 经销商名称是否存在
     *
     * @param shortName
     * @return
     */
    @Override
    public Boolean getShortNameExist(String shortName) {
        int countByShortName = dealerMapper.getCountByShortName(shortName);
        if (countByShortName > 0) {
            return true;
        }
        return false;
    }

    /**
     * 经销商全称是否存在
     *
     * @param fullName
     * @return
     */
    @Override
    public Boolean getFullNameExist(String fullName) {
        int countByFullName = dealerMapper.getCountByFullName(fullName);
        if (countByFullName > 0) {
            return true;
        }
        return false;
    }

    /**
     * 经销商数量
     *
     * @return
     */
    @Override
    public Integer getCount() {
        return dealerMapper.getCount();
    }

    /**
     * 获取所有经销商所在城市id
     *
     * @return
     */
    @Override
    public List<Integer> getRegionId() {
        return dealerMapper.getRegionId();
    }

    /**
     * 根据区域id查询
     *
     * @param regionId 区域id
     * @return
     */
    @Override
    public List<DealerDto> getDealerByRegionId(Integer regionId) {
        return Convert.toDealerDtoList(dealerMapper.getDealerByRegionId(Arrays.asList(regionId)));
    }

    @Override
    public List<DealerDto> getDealerByRegionId(List<Integer> regionIds) {
        return Convert.toDealerDtoList(dealerMapper.getDealerByRegionId(regionIds));
    }

    @Override
    public List<DealerDto> getDealerListByParam(Map<String, Object> param) {
        return Convert.toDealerDtoList(dealerMapper.getDealerListByParam(param));
    }

    @Override
    public Integer getOpenDealerCount() {
        return dealerMapper.getOpenDealerCount();
    }

    @Override
    public List<DealerDto> getDealerByShortNameFuzzy(String dealerName,Integer status, Integer type) {
        List<DealerBean> dealerBeanList = dealerMapper.getListByName(dealerName, status, type);
        return Convert.toDealerDtoList(dealerBeanList);
    }

}
